//
//  CreateJobValidSPVCheckView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/3/17.
//

import UIKit
import SnapKit
class CreateJobValidSPVCheckView: View {
    var closure: ((Bool)->Void)?

    override func setUpView() {
        self.backgroundColor = UIColor(hexStr: "#00000080")
        //back view
        let backView = UIView()
        backView.drawCorner(byRoundingCorners: .allCorners,radii: 10)
        backView.backgroundColor = UIColor.white
        self.addSubview(backView)
        backView.layer.cornerRadius = Constant.BackViewCornerRadius
        backView.snp.makeConstraints { (make) in
            make.width.equalTo(300)
            make.height.greaterThanOrEqualTo(0)
            make.center.equalToSuperview()
        }
        //title label
        let titleLabel = ContentLabel(R.font.interMedium(size: 15))
        titleLabel.text = "The current job is supported by the Solar Panel Validation function. Would you like to enable it?"
        titleLabel.numberOfLines = 0
        titleLabel.textAlignment = .center
        backView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.left.equalTo(24)
            make.right.equalTo(-24)
            make.height.greaterThanOrEqualTo(0)
        }
        //sub title label
        let subTitleLabel = ContentLabel(R.font.interRegular(size: 13),hexStr: "#292D32B2")
        subTitleLabel.text = "Enabling the Solar Panel Validation function will allow verification and validation of approved modules on the Clean Energy Council’s list."
        subTitleLabel.numberOfLines = 0
        subTitleLabel.textAlignment = .center
        backView.addSubview(subTitleLabel)
        subTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(16)
            make.left.right.equalTo(titleLabel)
            make.height.greaterThanOrEqualTo(0)
        }
        //link btn
        let spvLinkBtn = UIButton()
        spvLinkBtn.setImage(R.image.createjob_valid_spvcheck_link_icon(), for: UIControl.State.normal)
        backView.addSubview(spvLinkBtn)
        spvLinkBtn.snp.makeConstraints { make in
            make.top.equalTo(subTitleLabel.snp.bottom).offset(18)
            make.height.width.greaterThanOrEqualTo(0)
            make.centerX.equalToSuperview()
        }
        //horizonal line view
        let horizonalLineView = View(hexStr: "#0000001A")
        backView.addSubview(horizonalLineView)
        horizonalLineView.snp.makeConstraints { (make) in
            make.top.equalTo(spvLinkBtn.snp.bottom).offset(24)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        //vertial line view
        let vertialLineView = View(hexStr: "#0000001A")
        backView.addSubview(vertialLineView)
        vertialLineView.snp.makeConstraints { (make) in
            make.top.equalTo(horizonalLineView.snp.bottom)
            make.height.equalTo(48)
            make.width.equalTo(1)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        //No button
        let noBtn = ViewFactory.shared.generateButtonOnAlertView(R.string.localizable.no())
        backView.addSubview(noBtn)
        noBtn.snp.makeConstraints { (make) in
            make.top.equalTo(horizonalLineView)
            make.left.equalToSuperview()
            make.right.equalTo(vertialLineView)
            make.bottom.equalToSuperview()
        }
        
        //Yes button
        let yesBtn = ViewFactory.shared.generateButtonOnAlertView(R.string.localizable.yes())
        backView.addSubview(yesBtn)
        yesBtn.snp.makeConstraints { (make) in
            make.top.equalTo(horizonalLineView)
            make.left.equalTo(vertialLineView)
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        yesBtn.addTarget(self, action: #selector(yesBtnClick), for: UIControl.Event.touchUpInside)
        noBtn.addTarget(self, action: #selector(noBtnClick), for: UIControl.Event.touchUpInside)
        spvLinkBtn.addTarget(self, action: #selector(spvLinkBtnClick), for: UIControl.Event.touchUpInside)
        //
        
    }
    @objc private func yesBtnClick(){
        self.closure?(true)
        self.removeFromSuperview()
    }
    
    @objc private func noBtnClick(){
        self.closure?(false)
        self.removeFromSuperview()
    }
    @objc private func spvLinkBtnClick(){
        if let naviagtionVC =  self.firstNaviViewController(){
            let vc = AttachmentViewController()
            vc.attchmentType = .OtherLink
            vc.attachmentURL = APIConfig.SPVLink
            naviagtionVC.pushViewController(vc, animated: false)
        }
    }
}
